﻿﻿/**
 * 远光Gris平台3.0版本 http://www.ygsoft.com/ Grace [Gris Application Cloud-Computing
 * Extension]
 * 
 * 注册gridcoldesign控件
 * 
 * 变更版本：
 * qinzhenzhou@ygsoft.com 2017-03-13 创建
 */

/**
 * 注册gridcoldesign控件
 */
define(["qzz.querygrid",'ecp.utils',"bootstrap"], function(querygrid, utils) {
	var qzz = window.qzz;
	qzz.space("pertain");
	/**
	 * @class qzzgridFormat
	 */
	qzz.pertain.gridcoldesign = function(qzzgridObj, winOpt)/**@lends qzzgridFormat */{
		var dsselecter = "#colDesignDlg" + qzzgridObj.index;
		//init dom
		var $ess = $(dsselecter);
		$ess.remove();
		$ess = $(dsselecter);
		var winDiv = $("<div id=\"colDesignDlg"+qzzgridObj.index+"\"></div>");
		$("BODY").append(winDiv);
		var winStr =  
    		"<div id=\"colDesignSelect\" class=\"modal show\" tabindex=\"-1\" role=\"dialog\" aria-hidden=\"true\" data-backdrop=\"static\">\n" +
    		"    <div class=\"modal-dialog\" style=\"width:568px;\">\n" + 
    		"        <div class=\"modal-content\">\n" + 
    		"            <div class=\"modal-header modal_header_bgblue\">\n" + 
    		"                <button type=\"button\" class=\"close\" id=\"ptClose\" data-dismiss=\"modal\" aria-hidden=\"true\">&times;</button>\n" + 
    		"                <h4 class=\"modal-title\" style=\"color:#428bca;\">"+qzzgridObj.getI18n("colset","列设置")+"</h4>\n" + 
    		"            </div>\n" + 
    		"            <div class=\"modal-body\">\n" + 
    		"              <table border=\"0px\" cellpadding=\"0px\" cellspacing=\"0px\" height=\"\"> " +
    		"                <tr style='height:30px;vertical-align: top;'><td>" +
    		"                  <span id=\"upRow\" style=\"margin-right:8px;cursor:pointer;\"><li class=\"fa fa-arrow-circle-o-up\"></li>"+qzzgridObj.getI18n("moveUp","上移")+"</span> " +
    		"                  <span id=\"downRow\" style=\"margin-right:8px;cursor:pointer;\"><li class=\"fa fa-arrow-circle-o-down\"></li>"+qzzgridObj.getI18n("moveDown","下移")+"</span> " +
    		"                  <span id=\"firstRow\" style=\"margin-right:8px;cursor:pointer;\"><li class=\"fa fa-arrow-up\" style=\"border-top: 2px solid black;position: relative;top: 2px;\"></li>"+qzzgridObj.getI18n("moveFirst","最上")+"</span> " +
    		"                  <span id=\"lastRow\" style=\"margin-right:8px;cursor:pointer;\"><li class=\"fa fa-arrow-down\" style=\"border-bottom: 2px solid black;position: relative;top: -2px;\"></li>"+qzzgridObj.getI18n("moveLast","最下")+"</span> " +
    		"                  <span id=\"restoreDefault\" style=\"margin-right:8px;cursor:pointer;\"><li class=\"fa fa-refresh\"></li>"+qzzgridObj.getI18n("reset","重置")+"</span> " +
    		"                </td></tr>" +
    		"                <tr><td> " +
    		"                  <table id=\"gridFormat"+qzzgridObj.index+"\"><tr><td></td></tr></table> " +
    		"                </td></tr> " +
    		"              </table> " +
    		"            </div>\n" + 
    		"            <div class=\"modal-footer\">\n" + 
    		"               <button type=\"button\" class=\"btn btn-default\" id=\"ptCansel\" data-dismiss=\"modal\">"+qzzgridObj.getI18n("cancel","取消")+"</button>\n" + 
    		"               <button type=\"button\" class=\"btn btn-primary\" id=\"ptOk\" data-dismiss=\"modal\">"+qzzgridObj.getI18n("ok","确定")+"</button>\n" + 
    		"            </div>\n" + 
    		"        </div>\n" + 
    		"    </div>\n" + 
    		"</div>";
        winDiv.html(winStr);
        var option = {
			  "treeGrid":false,
			  "filter":false, 
			  "sort":false,
			  "hidePopMenu":true,
			  "height":"254",
			  "width":"538",
			  "suitToFit":true,
			  "autoEcpWidth":false,
			  "colNames":[qzzgridObj.getI18n("colName","列名"),	
			              qzzgridObj.getI18n("display","显示"),
			              qzzgridObj.getI18n("frozen","固定")],
			  "colModels":[
			               {"name":"rowName","width":172,"outputEscape":false},
			               {"name":"display","editType":"onoff","align":"center","width":78},
			               {"name":"frozen","editType":"onoff","align":"center","width":78}			               
			               ]
		}
        //有配置了分组开关，用户才可以配置自动合并内容
        if(qzzgridObj.getOption().groups === true) {
        	option.colNames.push(qzzgridObj.getI18n("groups","自动合并"));
        	option.colModels.push({"name":"groups","editType":"onoff","align":"center","width":78});
        }
		//render
		var headgrid = $("#gridFormat"+qzzgridObj.index).qzzquerygrid(option);
		
		var cns = qzzgridObj.priorColNames || qzzgridObj.getOption().colNames, 
		    cms = qzzgridObj.priorColModels || qzzgridObj.getOption().colModels
		var hvas = qzzgridObj.priorColNamesValue;
		if(hvas == null) {
		    hvas = qzzgridObj.colNamesToColDatas(cms, cns);
		}
		//var option = qzzgridObj.colDatasToColNames(hvas);
		headgrid.value(hvas, function() {
			headgrid.each(function(node) {
				var cm = node.data.colModel;
				if(cm.disGroup === true || cm.editType === "radio") {
					this.getCellOption("groups").disabled = true;
				}
			});
		});
		//移动.
		$("#upRow", winDiv).click(function() {
			headgrid.moveRecord(-1);	
		});
		$("#downRow", winDiv).click(function() {
			headgrid.moveRecord(1);
		});
		$("#firstRow", winDiv).click(function() {
			var count = headgrid.dataSet.getIndex() + 1;
			headgrid.moveRecord(count * -1);
		});
		$("#lastRow", winDiv).click(function() {
			var count = headgrid.getRecordCount() - headgrid.dataSet.getIndex() + 1;
			headgrid.moveRecord(count);
		});
		
		$("#ptOk", winDiv).click(function() {			
			var revas = headgrid.value();
			qzzgridObj.priorColNamesValue = revas;
			var cols = qzzgridObj.colDatasToColNames(revas);			
			qzzgridObj.refreshTitle(cols);
			qzzgridObj.first();
		});
		//恢复默认.
		$("#restoreDefault", winDiv).click(function() {			
			var dcns = qzzgridObj.extend([], qzzgridObj.getOption().defColNames), dcms = qzzgridObj.extend([], qzzgridObj.getOption().defColModels);
			var dhvas = qzzgridObj.colNamesToColDatas(dcms, dcns);
			qzzgridObj.priorColNamesValue = null;
			//var option = qzzgridObj.colDatasToColNames(hvas);
			headgrid.value(dhvas);		
		});
		
		headgrid.bind("onAfterEdit", function(dt, fieldName, value, text, cm, cNode) {
			var frozen = this.getValue("frozen", cNode);
			var display = this.getValue("display", cNode);
			if(fieldName == "frozen") {
				if(frozen === true && display !== true) {
					this.setValue("display", true, cNode);
				}
			} else if(fieldName == "display") {
				if(display !== true && frozen === true) {
					this.setValue("frozen", false, cNode);
				}
			}
		});
		qzzgridObj.focus = false;
		//headgrid.setFocus();
        $(dsselecter).modal();
        
        
	}
	return qzz.pertain.gridcoldesign;
});

